/**
 * 
 */
package com.gopay.solution.dao.ext.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.solution.PayChanExtConstants;
import com.gopay.common.domain.solution.SsTransPayChanDtlExt;
import com.gopay.common.exception.GopayException;
import com.gopay.solution.dao.ext.SsTransPayChanDtlExtDao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gaocl
 * @2016年8月4日 下午3:05:32 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository(value="ssTransPayChanDtlExtDao")
public class SsTransPayChanDtlExtDaoImpl extends BaseDAOImpl<SsTransPayChanDtlExt, Long> implements SsTransPayChanDtlExtDao {

    private Log logger = LogFactory.getLog(this.getClass());

    public SsTransPayChanDtlExt getExtVal(String acctId, String intTxnCd, String payChannel, String name)
            throws GopayException {

        Map<String, Object> params = new HashMap<>();
        params.put("acctId", acctId);
        params.put("intTxnCd", intTxnCd);
        params.put("payChannel", payChannel);
        params.put("name", name);
        params.put("type", PayChanExtConstants.PAY_CHAN_DTL_EXT_TYPE_PER);

        //先取个性化数据
        List<SsTransPayChanDtlExt> list =  findByHql(getSql(PayChanExtConstants.PAY_CHAN_DTL_EXT_TYPE_PER), params);

        if (list == null || list.size() == 0)  {
            
            params.put("name", PayChanExtConstants.IS_PERSONAL_SIGN);
            List<SsTransPayChanDtlExt> perList =  findByHql(getSql(PayChanExtConstants.PAY_CHAN_DTL_EXT_TYPE_PER), params);
           
            if(perList.size() > 0){
                throw new GopayException(ProcCodeConstants.PROC_CODE_100F1002, "做过个性化设置，但是清算渠道已关闭！"); 
            }

            logger.info("未取到个性化方案数据，开始获取通用方案数据");

            //个性化 数据 没取到  取通用
            params.remove("acctId");
            params.put("name", name);
            params.put("type", PayChanExtConstants.PAY_CHAN_DTL_EXT_TYPE_COM);

            //取通用数据
            list =  findByHql(getSql(PayChanExtConstants.PAY_CHAN_DTL_EXT_TYPE_COM), params);
            if(list == null || list.size() == 0) {
                // 通用也没有 要么参数错误  要么通用中没有选择支持的银行  抛出异常
                throw new GopayException(ProcCodeConstants.PROC_CODE_100F1002, "参数错误，或通用方案未初始化");
            } else {
                return  list.get(0);
            }
        } else {
            return list.get(0);
        }
    }


    private String getSql(String type)  {
        StringBuffer hql = new StringBuffer();
        hql.append(" from SsTransPayChanDtlExt " +
                " where gopayIntTxnCd = :intTxnCd" +
                " and payChannel = :payChannel" +
                " and name = :name" +
                " and type = :type ");

        if (type.equals(PayChanExtConstants.PAY_CHAN_DTL_EXT_TYPE_PER)) {
            hql.append(" and acctId = :acctId");
        }

        return hql.toString();


    }
}
